<template>
    <div>
        <!--高级查询-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="find">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--新增、编辑界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="员工姓名" prop="username">
                    <el-input v-model="addForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工密码" prop="password">
                    <el-input v-model="addForm.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工电话" prop="phone">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工年龄" prop="age">
                    <el-input v-model="addForm.age"></el-input>
                </el-form-item>
                <el-form-item label="员工邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="-1">待激活</el-radio>
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁止</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--列表-->
        <el-table :data="EmpData" stripe highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="username" label="姓名" width="120" sortable>
            </el-table-column>
            <el-table-column prop="age" label="年龄" width="100" sortable>
            </el-table-column>
            <el-table-column prop="password" label="密码" width="300" sortable>
            </el-table-column>
            <el-table-column prop="phone" label="电话" width="160" sortable>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="200" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="100" :formatter="formatState" sortable>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>
    </div>
</template>
<script>
    export default {
        data(){
            return {
                EmpData: [],
                listLoading: false,  // 默认不开启加载效果
                sels: [],
                total: 0, // 总数量
                query: {
                    pageSize: 10,
                    currentPage: 1,
                    keywords: ''
                },
                addFormVisible: false, // 默认不显示弹出框
                addLoading: false, // 默认不开启加载效果
                addFormRules: { // 规则
                    username: [
                        { required: true, message: '请输入员工姓名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入员工密码', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入员工电话', trigger: 'blur' }
                    ]
                },
                // 双向绑定
                addForm :{
                    username: '',
                    password: '',
                    phone: '',
                    email: '',
                    age: '',
                    state: -1
                },
            }
        },
        methods:{
            // 高级查询
            find(){
                // 设置默认页面为第一页
                this.query.currentPage=1;
                // 加载页面
                this.listEmp();
            },
            // 添加
            handleAdd(){
                // 清空内容
                this.addForm = {
                    username: '',
                    password: '',
                    phone: '',
                    email: '',
                    age: '',
                    state: -1
                }
                // 弹出添加框
                this.addFormVisible = true;
            },
            // 编辑
            handleEdit(index,row){
                // 弹出框
                this.addFormVisible = true;
                // 数据回显，Object.assign(a,b) 复制方法
                this.addForm = Object.assign({},row);
            },
            // 提交
            addSubmit(){
                // 开启加载效果
                listLoading: true;
                // 发送请求
                this.$http.put("/emp",this.addForm).then(res=>{
                    console.debug(res);
                    // 返回信息
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error('网络异常');
                    }
                    listLoading: false // 关闭加载效果
                    // 关闭弹出框
                    this.addFormVisible = false;
                    // 重新加载页面
                    this.listEmp();
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            },
            // 勾选
            selsChange(val){
                this.sels = val;
            },
            // 状态
            formatState: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '待激活';
            },
            // 删除
            handleDel(index,row){
                // 弹出确认框
                this.$confirm('确认删除？', '提示', {
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=> { // 选择确认的回调方法
                    this.$http.delete("/emp/"+row.id).then(res=>{
                        // 返回信息
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error('网络异常');
                        }
                        console.debug(success,message);
                        // 加载页面
                        this.listEmp();
                    })
                }).catch(()=>{ // 选择取消的回调方法
                    this.$message('已取消');
                })
            },
            // 批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                // 弹出确认框
                this.$confirm('确认删除？', '提示', {
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    this.$http.patch("/emp",ids).then(res=>{
                        // 返回信息
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error('网络异常');
                        }
                        console.debug(success,message);
                        // 加载页面
                        this.listEmp();
                    })
                }).catch(()=>{ // 选择取消的回调方法
                    this.$message('已取消');
                })
            },
            // 页面改变页数
            handleCurrentChange(val){
                this.query.currentPage=val;
                // 加载页面
                this.listEmp();
            },
            listEmp(){
                this.$http.post("/emp",this.query).then(res=>{
                    console.debug(res);
                    this.EmpData = res.data.data;
                    this.total = res.data.total;
                })
            }
        },
        mounted(){
            // 加载页面
            this.listEmp();
        }
    }
</script>

<style>

</style>